Ext.define("jwawa.erp.common.SearchPanelHandler", {
    mixins:['jwawa.erp.framework.HandlerUtil'],
    resources: {
        commonSearchModel: 'jwawa.erp.common.CommonSearchModel'
    },
    ref: {
        searchGroupCombobox: '#searchGroupCombobox',
        openAdvanceSearchWinButton: '#openAdvanceSearchWinButton',
        searchButton: '#searchButton',
        resetButton: '#resetButton'
    },
    constructor: function(params){
        "use strict";
        var me = this;

        Ext.apply(this, params);

        me.initResource();

    },

    bindHandler: function() {
        "use strict";
        var me = this,
            view = me.view,
            ref = me.ref,
            commonSearchModel = Ext.JwawaResMgr.get('commonSearchModel');

        me.initRef(view, ref);
        me.initSearchPanel();

        me.view.addListener('render', function(){
            commonSearchModel.loadSearchData(view.pageCode);
        });
    },

    initSearchPanel: function(){
        "use strict";
        var me = this,
            commonSearchModel = Ext.JwawaResMgr.get('commonSearchModel');

        me.searchGroupCombobox.bindStore(commonSearchModel.searchGroupStore);
        me.searchGroupCombobox.on('select', Ext.bind(me.onSelectSearchGroupCombobox, me));
        me.openAdvanceSearchWinButton.on('click', me.onClickOpenAdvanceSearchWinBtn, me);
        me.searchButton.on('click', me.onClickSearchButton, me);
        me.resetButton.on('click', me.onClickResetButton, me);
        commonSearchModel.addListener('loadSearchData', me.onLoadSearchData, me);
    },

    onLoadSearchData: function(){
        "use strict";
        var me = this,
            defaultSearchGroupId,
            commonSearchModel = Ext.JwawaResMgr.get('commonSearchModel'),
            searchItemList;

        defaultSearchGroupId = commonSearchModel.getDefaultSearchGroupId();
        me.searchGroupCombobox.setValue(defaultSearchGroupId);

        searchItemList = commonSearchModel.getSearchGroupItemShipListByGroupId(defaultSearchGroupId);
        me.resetSearchPanel(searchItemList);
    },

    onSelectSearchGroupCombobox:  function(combo, records, eOpts){
        "use strict";
        var me = this,
            searchGroupId = combo.getValue(),
            commonSearchModel = Ext.JwawaResMgr.get('commonSearchModel'),
            searchItemList;

        if(searchGroupId != 0){
            searchItemList = commonSearchModel.getSearchGroupItemShipListByGroupId(searchGroupId);
            me.resetSearchPanel(searchItemList);
        } else {
            me.resetSearchPanel();
        }
    },

    insertSearchItemIntoPanel: function(searchGroupItemShipList){
        "use strict";
        var me = this,
            view = me.view,
            commonSearchModel = Ext.JwawaResMgr.get('commonSearchModel');

        if(Ext.isEmpty(searchGroupItemShipList) || searchGroupItemShipList.length == 0){
            return;
        }

        for(var i = 0; i <  searchGroupItemShipList.length; i++){
            var searchGroupItemShip = searchGroupItemShipList[i],
                searchItemRec = commonSearchModel.searchItemStore.getById(searchGroupItemShip['search_item_id']);
            var itemCfg = Ext.apply({
                name: searchItemRec.get('entity_field_name'),
                fieldLabel: searchItemRec.get('entity_field_label'),
                value: searchGroupItemShip['default_value'],
                margin: '3',
                labelWidth: 60,
                columnWidth: 1 / view.rowFieldNum
            }, convertRawStrToJasn(searchItemRec.get('item_cfg')));

            var field = Ext.create(searchItemRec.get('item_class'), itemCfg);
            view.add(field);
        }
    },

    onClickResetButton: function(){
        "use strict";
        var me = this;

        me.onSelectSearchGroupCombobox(me.searchGroupCombobox, null, null);
    },

    onClickSearchButton:  function(){
        "use strict";
        var me = this,
            view = me.view;

//        var searchData = view.getForm().getValues(false, false, false, false);
        var searchData = {
            searchPageCode: me.view.pageCode,
            searchItems: view.getForm().getValues(false, false, false, false)
        };
        view.fireEvent('search', me, searchData);
    },

    resetSearchPanel: function(searchGroupItemShipList){
        "use strict";
        var me = this,
            view = me.view,
            widgetHeight = 22,
            widgetMargin = 3,
            padding = 10,
            widgetBox = 1,
            minHieght,
            toobarHeight = 27;

        me.emptySearchPanel();
        if(!Ext.isEmpty(searchGroupItemShipList) && searchGroupItemShipList.length > 0){
            me.insertSearchItemIntoPanel(searchGroupItemShipList);
        }

        var itemNum = view.items.items.length;
        minHieght = (widgetMargin * 2 + widgetHeight + widgetBox * 2) + 2 * padding + toobarHeight;
//        widgetHeight = view.items.items[0].el.getHeight();
        if(itemNum > 0) {
            var tmp = Math.ceil(itemNum / view.rowFieldNum);
            view.setHeight(tmp * (widgetMargin * 2 + widgetHeight + widgetBox * 2) + 2 * padding + toobarHeight);
        } else {
            view.setHeight(minHieght);
        }

        view.minHeight = minHieght;
    },

    emptySearchPanel: function(){
        "use strict";
        var me = this;

        me.view.removeAll();
    },

    onClickOpenAdvanceSearchWinBtn: function(){
        "use strict";
        var me = this;

        if (!me.advanceSearchWin){
            me.advanceSearchWin = Ext.create('jwawa.erp.common.AdvanceSearchWin', {
                searchPanel: me.view
            });
        }

        me.advanceSearchWin.show();
        me.advanceSearchWin.alignTo(Ext.getBody(), "t-t", [0, 20]);
    }
});
